﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ComponentModel;
using DevExpress.XtraEditors;
using MM.Common.Base;

namespace MM.Common.UI.Controls
{
    public class UCCheckBox : CheckEdit, IControlProperty, IControlField
    {
        // Fields
        private string _ControlCode;
        private string _ControlName;
        private EControlPropertyStatus _ControlProperty;
        private string _FieldDBName;
        private EControlDBType _fieldType;
        private bool _isFake;
        private bool _isIndex;
        private IContainer components;
        private bool isedited;

        // Methods
        public UCCheckBox()
        {
            this._FieldDBName = "";
            this.InitializeComponent();
            base.ErrorIconAlignment = System.Windows.Forms.ErrorIconAlignment.TopRight;
            base.PropertiesChanged += new EventHandler(this.UCCheckBox_PropertiesChanged);
        }

        public UCCheckBox(IContainer container)
        {
            this._FieldDBName = "";
            container.Add(this);
            this.InitializeComponent();
            base.ErrorIconAlignment = System.Windows.Forms.ErrorIconAlignment.TopRight;
            base.PropertiesChanged += new EventHandler(this.UCCheckBox_PropertiesChanged);
        }

        public void clearMe()
        {
            this.EditValue = null;
            this.isEdited = false;
        }

        protected override void Dispose(bool disposing)
        {
            if (disposing && (this.components != null))
            {
                this.components.Dispose();
            }
            base.Dispose(disposing);
        }

        private void InitializeComponent()
        {
            this.components = new Container();
        }

        protected override void OnEnabledChanged(EventArgs e)
        {
            base.OnEnabledChanged(e);
            if ((!string.IsNullOrEmpty(this.ControlCode) && (this.ControlProperty != EControlPropertyStatus.Write)) && base.Enabled)
            {
                base.Enabled = false;
            }
        }

        protected override void OnValidating(CancelEventArgs e)
        {
            base.OnValidating(e);
            this.isEdited = this.IsModified;
        }

        protected override void OnVisibleChanged(EventArgs e)
        {
            base.OnVisibleChanged(e);
            if ((!string.IsNullOrEmpty(this.ControlCode) && (this.ControlProperty == EControlPropertyStatus.Hide)) && base.Visible)
            {
                base.Visible = false;
            }
        }

        private void UCCheckBox_PropertiesChanged(object sender, EventArgs e)
        {
            if ((((e is PropertyChangedEventArgs) && (((PropertyChangedEventArgs)e).PropertyName == "ReadOnly")) && (!string.IsNullOrEmpty(this.ControlCode) && (this.ControlProperty != EControlPropertyStatus.Write))) && !base.Properties.ReadOnly)
            {
                base.Properties.ReadOnly = true;
            }
        }

        // Properties
        [Category("MM附加"), Description("控制本控件对应的系统代码"), DefaultValue((string)null), Browsable(true)]
        public string ControlCode
        {
            get
            {
                return this._ControlCode;
            }
            set
            {
                this._ControlCode = value;
            }
        }

        [Description("控制本控件对应的系统名称"), Browsable(true), DefaultValue((string)null), Category("MM附加")]
        public string ControlName
        {
            get
            {
                return this._ControlName;
            }
            set
            {
                this._ControlName = value;
                if (!base.DesignMode && !string.IsNullOrEmpty(this.ControlCode))
                {
                    if (this._ControlProperty == EControlPropertyStatus.None)
                    {
                        this._ControlProperty = BaseDataHelper.GetFunctionPointProperty(this._ControlCode, this._ControlName);
                    }
                    switch (this._ControlProperty)
                    {
                        case EControlPropertyStatus.Hide:
                            base.Visible = false;
                            return;

                        case EControlPropertyStatus.Read:
                            base.Properties.ReadOnly = true;
                            base.Visible = true;
                            return;

                        case EControlPropertyStatus.Write:
                            base.Properties.ReadOnly = false;
                            base.Visible = true;
                            return;

                        default:
                            return;
                    }
                }
            }
        }

        [Browsable(false), DefaultValue(0)]
        public EControlPropertyStatus ControlProperty
        {
            get
            {
                return this._ControlProperty;
            }
            set
            {
            }
        }

        [Browsable(true), Description("本控件在数据库中对应的字段名"), DefaultValue(""), Category("MM数据库")]
        public string FieldDBName
        {
            get
            {
                return this._FieldDBName;
            }
            set
            {
                this._FieldDBName = value;
            }
        }

        [DefaultValue(0), Description("字段在数据库中的类型"), Category("MM数据库"), Browsable(true)]
        public EControlDBType FieldType
        {
            get
            {
                return this._fieldType;
            }
            set
            {
                this._fieldType = value;
            }
        }

        [Description("字段的值"), Category("MM数据库"), DefaultValue("N"), Browsable(true)]
        public object FieldValue
        {
            get
            {
                if (this.Checked)
                {
                    return "Y";
                }
                return "N";
            }
            set
            {
                if (value == null)
                {
                    this.Checked = false;
                }
                else if (value.ToString().Trim().ToUpper() == "Y")
                {
                    this.Checked = true;
                }
                else if (value.ToString().Trim().ToUpper() == "N")
                {
                    this.Checked = false;
                }
            }
        }

        [Browsable(true), DefaultValue((string)null), Category("MM数据绑定"), Description("将编辑控件进行分组管理")]
        public string GroupName { get; set; }

        [Browsable(false), DefaultValue(false)]
        public bool isEdited
        {
            get
            {
                return this.isedited;
            }
            set
            {
                this.isedited = value;
            }
        }

        [DefaultValue(false), Description("在数据库中并不存在，但在界面中存在的显示控件"), Category("MM数据库"), Browsable(true)]
        public bool IsFake
        {
            get
            {
                return this._isFake;
            }
            set
            {
                this._isFake = value;
            }
        }

        [Description("该字段是否主键"), Browsable(true), DefaultValue(false), Category("MM数据库")]
        public bool IsIndex
        {
            get
            {
                return this._isIndex;
            }
            set
            {
                this._isIndex = value;
            }
        }
    }
}
